package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.TtWhmCountDetailBean;
import com.efast.dmom.mgmt.bean.les.TtWhmCountHeaderDetailBean;
import com.efast.dmom.mgmt.entity.les.TtWhmCountDetail;
import com.efast.dmom.mgmt.entity.les.TtWhmCountHeader;
import com.efast.dmom.mgmt.service.les.TtWhmCountDetailService;
import com.efast.dmom.mgmt.service.les.TtWhmCountHeaderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 盘点头表 前端控制器
 * </p>
 *
 * @author ChenLiPeng
 * @since 2018-06-12
 */
@Controller
@RequestMapping("/pdaTtWhmCountHeader")
@Api(description = "库内->pda->库存盘点-初盘")
public class PdaTtWhmCountHeaderController {

	/**
	 * 盘点头
	 */
	private final TtWhmCountHeaderService ttWhmCountHeaderService;
	/**
	 * 盘点明细
	 */
	private final TtWhmCountDetailService ttWhmCountDetailService;

	@Autowired
	public PdaTtWhmCountHeaderController(TtWhmCountHeaderService ttWhmCountHeaderService,
			TtWhmCountDetailService ttWhmCountDetailService) {
		this.ttWhmCountHeaderService = ttWhmCountHeaderService;
		this.ttWhmCountDetailService = ttWhmCountDetailService;
	}

	/**
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	@ApiOperation(value = "【根据单号查询盘点明细的接口】", notes = "请求参数：")
	public @ResponseBody ResultBean add(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("count_task_no", ttWhmCountHeader.getCountTaskNo());
		List<TtWhmCountHeader> ttWhmCountHeaders = ttWhmCountHeaderService.selectByMap(stringObjectMap);
		if (ttWhmCountHeaders.size() == 0) {
			throw new ServiceException("没有查询到对应的数据");
		}
		Map<String, Object> stringObjectMap1 = new HashMap<>(16);
		stringObjectMap1.put("tt_whm_count_header_id", ttWhmCountHeaders.get(0).getTtWhmCountHeaderId());
		List<TtWhmCountDetail> ttWhmCountDetails = ttWhmCountDetailService.selectByMap(stringObjectMap1);
		if (ttWhmCountDetails.size() == 0) {
			throw new ServiceException("没有查询到对应的数据");
		}
		TtWhmCountHeaderDetailBean ttWhmCountHeaderDetailBean = new TtWhmCountHeaderDetailBean();
		ttWhmCountHeaderDetailBean.setTtWhmCountHeader(ttWhmCountHeaders.get(0));
		ttWhmCountHeaderDetailBean.setTtWhmCountDetails(ttWhmCountDetails);
		result.setObject(ttWhmCountHeaderDetailBean);
		return result;
	}

	/**
	 *
	 * @param ttWhmCountDetailBean
	 *            ttWhmCountDetailBean
	 */
	@RequestMapping(value = "/confirm", method = RequestMethod.POST)
	@ApiOperation(value = "【初盘】", notes = "请求参数：明细的数组,实际盘点箱数，数量。已经明细的id")
	public @ResponseBody ResultBean confirm(@RequestBody TtWhmCountDetailBean ttWhmCountDetailBean) {
		ResultBean result = new ResultBean(true, "'");
		ttWhmCountHeaderService.confirm(ttWhmCountDetailBean);
		return result;
	}


	/**
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/repeatQuery", method = RequestMethod.POST)
	@ApiOperation(value = "【复盘查询的接口】", notes = "请求参数：")
	public @ResponseBody ResultBean repeatQuery(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("count_task_no", ttWhmCountHeader.getCountTaskNo());
		List<TtWhmCountHeader> ttWhmCountHeaders = ttWhmCountHeaderService.selectByMap(stringObjectMap);
		if (ttWhmCountHeaders.size() == 0) {
			throw new ServiceException("没有查询到对应的数据");
		}
		Map<String, Object> stringObjectMap1 = new HashMap<>(16);
		stringObjectMap1.put("tt_whm_count_header_id", ttWhmCountHeaders.get(0).getTtWhmCountHeaderId());
		// 复盘查询的时候只能查询到盘点中的明细
		stringObjectMap1.put("count_status","1");
		List<TtWhmCountDetail> ttWhmCountDetails = ttWhmCountDetailService.selectByMap(stringObjectMap1);
		if (ttWhmCountDetails.size() == 0) {
			throw new ServiceException("没有查询到对应的数据");
		}
		TtWhmCountHeaderDetailBean ttWhmCountHeaderDetailBean = new TtWhmCountHeaderDetailBean();
		ttWhmCountHeaderDetailBean.setTtWhmCountHeader(ttWhmCountHeaders.get(0));
		ttWhmCountHeaderDetailBean.setTtWhmCountDetails(ttWhmCountDetails);
		result.setObject(ttWhmCountHeaderDetailBean);
		return result;
	}

	/**
	 *
	 * @param ttWhmCountDetailBean
	 *            ttWhmCountDetailBean
	 */
	@RequestMapping(value = "/repeatConfirm", method = RequestMethod.POST)
	@ApiOperation(value = "【复盘】", notes = "请求参数：明细的数组")
	public @ResponseBody ResultBean repeatConfirm(@RequestBody TtWhmCountDetailBean ttWhmCountDetailBean) {
		ResultBean result = new ResultBean(true, "'");
		ttWhmCountHeaderService.repeatConfirm(ttWhmCountDetailBean);
		return result;
	}

}
